Imports System.Data
Partial Class Default2
    Inherits System.Web.UI.Page


    Protected Sub ddlContractCarrier_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                                                            Handles ddlContractCarrier.SelectedIndexChanged
        Message.Text = ""
        gdvwContractRates.DataBind()

        Dim ContractRateINFOLogic As New GTSSystem.BLL.Rates
        Dim RateData As DataTable

        'Create the table to return to the Shipment.BLL
        Dim RData As New DataTable("Rates")
        RData.Columns.Add("Rate Detail ID", GetType(Integer))
        RData.Columns.Add("Origin", GetType(String))
        RData.Columns.Add("Destination", GetType(String))
        RData.Columns.Add("Service Type", GetType(String))
        RData.Columns.Add("Minimum Weight", GetType(Double))
        RData.Columns.Add("Maximum Weight", GetType(Double))
        RData.Columns.Add("Rate Charge", GetType(Double))
        RData.Columns.Add("Base Rate", GetType(Double))
        RData.Columns.Add("Minimum Charge", GetType(Double))
        RData.Columns.Add("Rate Type", GetType(String))

        RateData = ContractRateINFOLogic.GetContractRateINFOByContractID(ddlContractCarrier.SelectedValue)
        If RateData.Rows.Count > 0 Then
            For n As Integer = 0 To RateData.Rows.Count - 1
                Dim RateDetailID As Integer = RateData.Rows(n).Item("RateDetailID")
                Dim Origin As Integer = RateData.Rows(n).Item("Origin")
                Dim Destination As Integer = RateData.Rows(n).Item("Destination")
                Dim ServiceTypeID As Integer = RateData.Rows(n).Item("ServiceTypeID")
                Dim RateTypeID As Integer = RateData.Rows(n).Item("RateTypeID")
                Dim MinWeight As Double = RateData.Rows(n).Item("MinWeight")
                Dim MaxWeight As Double = RateData.Rows(n).Item("MaxWeight")
                Dim RateCharge As Double = 0
                Dim BaseRate As Double = 0
                Dim MinCharge As Double = 0
                If Not IsDBNull(RateData.Rows(n).Item("Price")) Then
                    RateCharge = RateData.Rows(n).Item("Price")
                End If
                If Not IsDBNull(RateData.Rows(n).Item("BaseRate")) Then
                    BaseRate = RateData.Rows(n).Item("BaseRate")
                End If
                If Not IsDBNull(RateData.Rows(n).Item("MinCharge")) Then
                    MinCharge = RateData.Rows(n).Item("MinCharge")
                End If

                'gets location names
                Dim OData As DataTable
                Dim DData As DataTable
                Dim LocationLogic As New GTSSystem.BLL.RateLocation()
                OData = LocationLogic.GetRateLocationByRateLocationID(Origin)
                DData = LocationLogic.GetRateLocationByRateLocationID(Destination)
                Dim OCity As String = OData.Rows(0).Item("City")
                Dim OProv As String = OData.Rows(0).Item("Province")
                Dim DCity As String = DData.Rows(0).Item("City")
                Dim DProv As String = DData.Rows(0).Item("Province")
                'gets service type name
                Dim STData As DataTable
                STData = GTSSystem.BLL.ServiceType.GetServiceTypeByID(ServiceTypeID)
                Dim ServiceType As String = STData.Rows(0).Item("ServiceTypeName")
                'gets rate type name
                Dim RTData As DataTable
                RTData = GTSSystem.BLL.RateType.GetRateTypeByID(RateTypeID)
                Dim RateType As String = RTData.Rows(0).Item("RateTypeName")
                Dim aRow As DataRow
                aRow = RData.NewRow
                aRow.Item("Rate Detail ID") = RateDetailID
                aRow.Item("Origin") = OCity + ", " + OProv
                aRow.Item("Destination") = DCity + ", " + DProv
                aRow.Item("Service Type") = ServiceType
                aRow.Item("Minimum Weight") = MinWeight
                aRow.Item("Maximum Weight") = MaxWeight
                aRow.Item("Rate Charge") = RateCharge
                aRow.Item("Base Rate") = BaseRate
                aRow.Item("Minimum Charge") = MinCharge
                aRow.Item("Rate Type") = RateType
                RData.Rows.Add(aRow)
            Next
            gdvwContractRates.DataSource = RData
            gdvwContractRates.DataBind()
        Else
            Message.Text = "No rates found for this contract"
        End If

    End Sub

    'Protected Sub gdvwContractRates_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) _
    '                                            Handles gdvwContractRates.RowEditing
    '    gdvwContractRates.EditIndex = e.NewEditIndex()

    'End Sub


    Protected Sub lbCommit_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                    Handles lbCommit.Click
        If txtPercent.Text.Trim = "" Or txtPercent.Text.Trim <= 0 Or txtPercent.Text.Trim > 100 Then
            Message.Text = "Percent change must be a number between 1 and 100"
        Else
            Dim ContractRateINFOLogic As New GTSSystem.BLL.Rates
            Dim RateData As DataTable

            'Create the table to return to the Shipment.BLL
            Dim RData As New DataTable("Rates")
            RData.Columns.Add("Rate Detail ID", GetType(Integer))
            RData.Columns.Add("Origin", GetType(String))
            RData.Columns.Add("Destination", GetType(String))
            RData.Columns.Add("Service Type", GetType(String))
            RData.Columns.Add("Minimum Weight", GetType(Double))
            RData.Columns.Add("Maximum Weight", GetType(Double))
            RData.Columns.Add("Rate Charge", GetType(Double))
            RData.Columns.Add("Base Rate", GetType(Double))
            RData.Columns.Add("Minimum Charge", GetType(Double))
            RData.Columns.Add("Rate Type", GetType(String))

            RateData = ContractRateINFOLogic.GetContractRateINFOByContractID(ddlContractCarrier.SelectedValue)
            If RateData.Rows.Count > 0 Then
                For n As Integer = 0 To RateData.Rows.Count - 1
                    Dim RateDetailID As Integer = RateData.Rows(n).Item("RateDetailID")
                    Dim Origin As Integer = RateData.Rows(n).Item("Origin")
                    Dim Destination As Integer = RateData.Rows(n).Item("Destination")
                    Dim ServiceTypeID As Integer = RateData.Rows(n).Item("ServiceTypeID")
                    Dim RateTypeID As Integer = RateData.Rows(n).Item("RateTypeID")
                    Dim MinWeight As Double = RateData.Rows(n).Item("MinWeight")
                    Dim MaxWeight As Double = RateData.Rows(n).Item("MaxWeight")
                    Dim RateCharge As Double = 0
                    Dim BaseRate As Double = 0
                    Dim MinCharge As Double = 0
                    If Not IsDBNull(RateData.Rows(n).Item("Price")) Then
                        RateCharge = RateData.Rows(n).Item("Price")
                    End If
                    If Not IsDBNull(RateData.Rows(n).Item("BaseRate")) Then
                        BaseRate = RateData.Rows(n).Item("BaseRate")
                    End If
                    If Not IsDBNull(RateData.Rows(n).Item("MinCharge")) Then
                        MinCharge = RateData.Rows(n).Item("MinCharge")
                    End If

                    'gets location names
                    Dim OData As DataTable
                    Dim DData As DataTable
                    Dim LocationLogic As New GTSSystem.BLL.RateLocation()
                    OData = LocationLogic.GetRateLocationByRateLocationID(Origin)
                    DData = LocationLogic.GetRateLocationByRateLocationID(Destination)
                    Dim OCity As String = OData.Rows(0).Item("City")
                    Dim OProv As String = OData.Rows(0).Item("Province")
                    Dim DCity As String = DData.Rows(0).Item("City")
                    Dim DProv As String = DData.Rows(0).Item("Province")
                    'gets service type name
                    Dim STData As DataTable
                    STData = GTSSystem.BLL.ServiceType.GetServiceTypeByID(ServiceTypeID)
                    Dim ServiceType As String = STData.Rows(0).Item("ServiceTypeName")
                    'gets rate type name
                    Dim RTData As DataTable
                    RTData = GTSSystem.BLL.RateType.GetRateTypeByID(RateTypeID)
                    Dim RateType As String = RTData.Rows(0).Item("RateTypeName")
                    ' insert calculation and update logic here
                    Dim p As Decimal = Convert.ToDecimal(txtPercent.Text) / 100
                    Dim RateLogic As New GTSSystem.BLL.Rates()
                    If radDirection.SelectedValue = "1" Then  'increase
                        RateCharge = RateCharge + (RateCharge * p)
                        Select Case RateType
                            Case Is = "Base Rate"
                                BaseRate = BaseRate + (BaseRate * p)
                                RateLogic.UpdateBaseRate(RateDetailID, RateCharge, BaseRate)
                            Case Is = "Fixed"
                                RateLogic.UpdateFixedRate(RateDetailID, RateCharge)
                            Case Is = "Incremental"
                                MinCharge = MinCharge + (MinCharge * p)
                                RateLogic.UpdateIncrementalRate(RateDetailID, RateCharge, MinCharge)
                        End Select
                    Else
                        RateCharge = RateCharge - (RateCharge * p)
                        Select Case RateType
                            Case Is = "Base Rate"
                                BaseRate = BaseRate - (BaseRate * p)
                                RateLogic.UpdateBaseRate(RateDetailID, RateCharge, BaseRate)
                            Case Is = "Fixed"
                                RateLogic.UpdateFixedRate(RateDetailID, RateCharge)
                            Case Is = "Incremental"
                                MinCharge = MinCharge - (MinCharge * p)
                                RateLogic.UpdateIncrementalRate(RateDetailID, RateCharge, MinCharge)
                        End Select
                    End If

                    Dim aRow As DataRow
                    aRow = RData.NewRow
                    aRow.Item("Rate Detail ID") = RateDetailID
                    aRow.Item("Origin") = OCity + ", " + OProv
                    aRow.Item("Destination") = DCity + ", " + DProv
                    aRow.Item("Service Type") = ServiceType
                    aRow.Item("Minimum Weight") = MinWeight
                    aRow.Item("Maximum Weight") = MaxWeight
                    aRow.Item("Rate Charge") = RateCharge
                    aRow.Item("Base Rate") = BaseRate
                    aRow.Item("Minimum Charge") = MinCharge
                    aRow.Item("Rate Type") = RateType
                    RData.Rows.Add(aRow)
                Next
                gdvwContractRates.DataSource = RData
                gdvwContractRates.DataBind()
                txtPercent.Text = ""
                Message.Text = "Rates updated"
            Else
                Message.Text = "No rates found for this contract"
            End If
        End If
    End Sub

End Class
